<script>
import { cloud } from '@/framework/utils/request'
import {
  addAd,
  editAd,
  getAdDetail,
  getAdList,
  getAdvertNameList,
  getGoodsList,
  getMyMallInfo
} from '@/api/mallApi/advert'

export default {
  name: 'AdvertPage',
  data() {
    return {
      isEdit: false,
      goodsList: [],
      dialogImageUrl: '',
      dialogVisible: false,
      label: '',
      fileList: [],
      uploadUrl: `${this.baseURL()}/sysFileInfo/upload?secretFlag=N&fileBucket=mall`,
      addForm: {
        // 广告名称
        adName: '',
        spaceId: '',
        // mark: '',
        adType: '',
        adShowId: '',
        startTime: '',
        endTime: '',
        img: '',
        status: 1 // 1:正常 2：费用用完 3：违规下架
      },
      spaceNameList: [],
      addShow: false,
      isShow: true,
      form: {
        // 广告名称
        adName: '',
        // 广告位名字
        spaceName: '',
        // 广告用户
        userName: '',
        // 有效期
        validDate: ''
      },
      // 表格数据
      tableData: [],
      // 分页数据
      currentPage: 1, // 当前页
      pageSize: 10, // 每页显示条数
      total: 0, // 总条数
      // 表格加载状态
      tableLoading: false,
      addRules: {
        adName: [
          { required: true, message: '请输入广告名称', trigger: 'blur' }
        ],
        spaceName: [
          { required: true, message: '请选择广告位', trigger: 'change' }
        ],
        startTime: [
          { required: true, message: '请选择开始时间', trigger: 'change' }
        ],
        endTime: [
          { required: true, message: '请选择结束时间', trigger: 'change' }
        ],
        img: [{ required: true, message: '请上传图片', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    baseURL() {
      return process.env.VUE_APP_BASE_API + cloud.file
    },
    handleClear() {
      this.form.adName = ''
      this.form.spaceName = ''
      this.form.userName = ''
      this.form.validDate = ''
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },
    handleAdd() {
      this.addShow = true
      getAdvertNameList().then((res) => {
        if (res.code === '00000') {
          console.log('res: ', res)
          this.spaceNameList = res.data
        }
      })
    },
    handleEdit(index, row) {
      this.addShow = true
      this.isEdit = true
      this.dialogVisible = true
      getAdDetail({ id: row.id }).then((res) => {
        if (res.code === '00000') {
          this.addForm = res.data
          this.addForm.ids = [res.data.id]
        }
        if (this.addForm.adType === 1) {
          this.label = '商品'
          getGoodsList()
            .then((res) => {
              if (res.code === '00000') {
                this.goodsList = res.data.rows
              }
            })
            .catch((err) => {})
        }
        if (this.addForm.adType === 2) {
          this.label = '店铺'
          getMyMallInfo()
            .then((res) => {
              if (res.code === '00000') {
                this.goodsList = res.data.rows.map((item) => {
                  return {
                    id: item.id,
                    name: item.mallName
                  }
                })
              }
            })
            .catch((err) => {})
        }
      })
    },
    handlePriceRank(index, row) {
      console.log(index, row)
    },
    handleChange() {
      if (this.addForm.adType === 1) {
        this.label = '商品'
        getGoodsList()
          .then((res) => {
            if (res.code === '00000') {
              this.goodsList = res.data.rows
            }
          })
          .catch((err) => {})
      }
      if (this.addForm.adType === 2) {
        this.label = '店铺'
        getMyMallInfo()
          .then((res) => {
            if (res.code === '00000') {
              this.goodsList = res.data.rows.map((item) => {
                return {
                  id: item.id,
                  name: item.mallName
                }
              })
            }
          })
          .catch((err) => {})
      }
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleRemove(file, fileList) {},
    handleSuccess(response, file, fileList) {
      // 将fileList赋值给this.fileList
      this.fileList = fileList
      // 将响应数据中的fileUrl赋值给addForm.img
      this.addForm.img = response.data.fileUrl
    },
    getList() {
      this.tableLoading = true
      const params = {
        pageSize: this.pageSize,
        pageNo: this.currentPage,
        // 广告名称
        adName: this.form.adName,
        // 广告位名字
        spaceName: this.form.spaceName,
        // 广告用户
        userName: this.form.userName
      }
      getAdList(params)
        .then((res) => {
          if (res.code === '00000') {
            this.tableData = res.data.list
            this.total = parseInt(res.data.total)
            this.tableLoading = false
          }
        })
        .catch((err) => {
          this.tableLoading = false
        })
    },
    handleCancel() {
      this.addForm = {}
      this.$refs.addForm.resetFields()
      this.addShow = false
      this.isEdit = false
    },
    handleCommit() {
      if (this.isEdit === true) {
        editAd(this.addForm)
          .then((res) => {
            // 关闭弹窗
            this.addShow = false
            this.isEdit = false
            // 提交成功
            this.$message({
              message: '编辑成功',
              type: 'success'
            })
            // 重置表单
            this.$refs.addForm.resetFields()
            // 重新加载数据
            this.getList()
          })
          .catch((err) => {})
      } else {
        this.$refs.addForm.validate((valid) => {
          if (valid) {
            addAd(this.addForm)
              .then((res) => {
                if (res.code === '00000') {
                  // 关闭弹窗
                  this.addShow = false
                  // 提交成功
                  this.$message({
                    message: '提交成功',
                    type: 'success'
                  })
                  // 重置表单
                  this.$refs.addForm.resetFields()
                  // 重新加载数据
                  this.getList()
                }
              })
              .catch((err) => {})
          }
        })
      }
    },
    handleSearch() {
      this.getList()
    },
    handleSwitch(row) {
      const params = {
        ids: [row.id],
        ...row
      }
      editAd(params)
        .then((res) => {
          if (res.code === '00000') {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
          }
          // 重新加载数据
          this.getList()
        })
        .catch((err) => {})
    }
  }
}
</script>

<template>
  <div>
    <div class="topSearchFive">
      <div class="topSearchFiveLeft">
        <div>
          <p style="margin-right: 20px">广告名称:</p>
          <el-input
            style="width: 260px"
            placeholder="广告名称"
            v-model="form.adName"
          ></el-input>
        </div>
        <div>
          <p style="margin-right: 20px">广告位名字:</p>
          <el-input
            style="width: 260px"
            placeholder="广告位名字"
            v-model="form.spaceName"
          ></el-input>
        </div>
        <div>
          <p style="margin-right: 20px">广告用户:</p>
          <el-input
            style="width: 260px"
            placeholder="广告用户"
            v-model="form.userName"
          ></el-input>
        </div>
      </div>
      <div class="topSearchFiveRight">
        <el-button
          type="primary"
          :style="{ marginLeft: '20px' }"
          @click="handleSearch"
        >
          查询
        </el-button>
        <el-button :style="{ marginLeft: '20px' }" @click="handleClear">
          重置
        </el-button>
      </div>
    </div>

    <div class="advert">
      <!-- 过度效果
      <transition name="form-enter-active form-leave-active">
        <div class="form" ref="form" v-if="isShow">
          <el-form :inline="true" :model="form">
            <el-form-item label="广告名称" label-width="100px">
              <el-input
                style="width: 300px"
                placeholder="广告名称"
                v-model="form.adName"
              ></el-input>
            </el-form-item>
            <el-form-item label="广告位名字" label-width="100px">
              <el-input
                style="width: 300px"
                placeholder="广告位名字"
                v-model="form.spaceName"
              ></el-input>
            </el-form-item>
            <el-form-item label="广告用户" label-width="100px">
              <el-input
                style="width: 300px"
                placeholder="广告用户"
                v-model="form.userName"
              ></el-input>
            </el-form-item>
                 <el-form-item label="有效期" label-width="100px">
                      <el-date-picker
                        v-model="form.validDate"
                 type="daterange"
                         style="width: 500px"
                          range-separator="至"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期"
                          value-format="yyyy-MM-dd"
                          format="yyyy-MM-dd"
                        ></el-date-picker>
                      </el-form-item>
            <el-form-item>
              <el-button
                @click="handleSearch"
                style="
                  background-color: #eb6009;
                  color: #fff;
                  margin-left: 40px;
                  font-weight: bold;
                "
                ><i style="margin-right: 6px" class="el-icon-search"></i>搜索
              </el-button>
              <el-button @click="handleClear">清空</el-button>
            </el-form-item>
          </el-form>
        </div>
      </transition> -->
      <!-- /.form -->
      <transition name="button-enter-active button-leave-active">
        <!-- <div class="button" ref="button">

          <el-button icon="el-icon-refresh" circle @click="getList"></el-button>
          <el-button -->
        <!-- @click="handleChangeShow" -->
        <!-- icon="el-icon-search" -->
        <!-- circle -->
        <!-- ></el-button>
        </div> -->
        <div class="SecondRow">
          <div class="SecondRowLeft">
            <el-button @click="handleAdd" type="primary">新增 </el-button>
          </div>
          <div class="SecondRowRight">
            <el-button circle icon="el-icon-refresh-right" @click="getList">
            </el-button>
          </div>
        </div>
        <!-- /.button -->
      </transition>
      <div class="table">
        <el-table
          :data="tableData"
          style="width: 100%"
          border
          stripe
          v-loading="tableLoading"
        >
          <!-- 广告名称、广告位名、广告类型、开始时间、结束时间、广告上下架状态、广告状态、广告用户、操作（编辑竞和价排行） -->
          <!-- 序号 -->
          <el-table-column
            align="center"
            type="index"
            label="排序"
            min-width="50"
          ></el-table-column>
          <el-table-column
            align="center"
            label="广告名称"
            prop="adName"
          ></el-table-column>
          <el-table-column
            align="center"
            label="广告位名"
            prop="spaceName"
          ></el-table-column>
          <el-table-column
            align="center"
            label="广告类型"
            prop="adType"
            min-width="80px"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.adType === 1">商品详情</span>
              <span v-else-if="scope.row.adType === 2">店铺</span>
              <span v-else-if="scope.row.adType === 3">促销活动</span>
              <span v-else-if="scope.row.adType === 4">秒杀活动</span>
              <span v-else-if="scope.row.adType === 5">拼团</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="开始时间"
            prop="startTime"
          ></el-table-column>
          <el-table-column
            align="center"
            label="结束时间"
            prop="endTime"
          ></el-table-column>
          <el-table-column align="center" label="广告上下架状态" prp="status">
            <template slot-scope="scope">
              <span v-if="scope.row.status === 1">正常使用</span>
              <span v-else-if="scope.row.status === 2">广告比不足下架</span>
              <span v-else-if="scope.row.status === 3">违规下架</span>
              <span v-else>广告已下架</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="广告状态" prop="enable">
            <template slot-scope="scope">
              <el-switch
                @change="handleSwitch(scope.row)"
                v-model="scope.row.enable"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="2"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="广告用户"
            prop="userName"
          ></el-table-column>
          <el-table-column align="center" label="操作" min-width="80px">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="handleEdit(scope.$index, scope.row)"
              >编辑
              </el-button>
              <!--            <el-button-->
              <!--              type="text"-->
              <!--              size="small"-->
              <!--              @click="handlePriceRank(scope.$index, scope.row)"-->
              <!--            >竞价排行-->
              <!--            </el-button>-->
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="end" style="margin-bottom: 20px">
          <el-pagination
            background
            style="margin-top: 20px"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </el-row>
      </div>
      <!-- /.table -->
      <!-- 新增弹窗 -->
      <el-dialog title="新增广告" :visible.sync="addShow" width="50%">
        <el-form
          :model="addForm"
          ref="addForm"
          :rules="addRules"
          label-width="100px"
        >
          <el-form-item label="广告名称" prop="adName">
            <el-input clearable v-model="addForm.adName"></el-input>
          </el-form-item>
          <!-- 请选择广告位 -->
          <el-form-item label="广告位" prop="spaceId">
            <el-select
              :disabled="isEdit"
              clearable
              v-model="addForm.spaceId"
              placeholder="请选择广告位"
            >
              <el-option
                v-for="item in spaceNameList"
                :key="item.id"
                :label="item.adSpaceName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- 标记 -->
          <!--          <el-form-item label="标记">-->
          <!--            <el-radio v-model="addForm.mark" label="1">手机</el-radio>-->
          <!--            <el-radio v-model="addForm.mark" label="2">电脑</el-radio>-->
          <!--          </el-form-item>-->
          <!-- 请选择广告类型 -->
          <el-form-item label="广告类型">
            <el-select
              :disabled="isEdit"
              clearable
              @change="handleChange"
              v-model="addForm.adType"
              placeholder="请选择广告类型"
            >
              <el-option label="商品详情" :value="1"></el-option>
              <el-option label="店铺" :value="2"></el-option>
              <!--<el-option label="促销活动" value="促销"></el-option>-->
              <!--<el-option label="秒杀活动" value="秒杀"></el-option>-->
              <!--<el-option label="拼团" value="拼团"></el-option>-->
            </el-select>
          </el-form-item>
          <el-form-item v-if="addForm.adType" :label="`选择${label}`">
            <el-select
              :disabled="isEdit"
              clearable
              v-model="addForm.adShowId"
              :placeholder="`请选择${label}`"
            >
              <el-option
                v-for="item in goodsList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- 广告上下架状态 -->
          <el-form-item v-show="isEdit" label="上下架状态">
            <el-radio v-model="addForm.status" :label="1">正常使用</el-radio>
            <el-radio v-model="addForm.status" :label="2"
            >广告比不足下架
            </el-radio>
            <el-radio v-model="addForm.status" :label="3">违规下架</el-radio>
          </el-form-item>
          <!--开始时间 -->
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker
              type="datetime"
              clearable
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="addForm.startTime"
            ></el-date-picker>
          </el-form-item>
          <!--结束时间 -->
          <el-form-item label="结束时间" prop="endTime">
            <el-date-picker
              type="datetime"
              clearable
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间"
              v-model="addForm.endTime"
            ></el-date-picker>
          </el-form-item>
          <!-- 上传图片 -->
          <el-form-item label="上传图片">
            <el-upload
              style="display: inline"
              :action="uploadUrl"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              :show-file-list="false"
            >
              <el-tooltip
                effect="dark"
                content="最佳尺寸：750像素 x 370像素"
                placement="top"
              >
                <el-input
                  placeholder="最佳尺寸：750像素 x 370像素"
                  style="width: 500px"
                  v-model="addForm.img"
                ></el-input>
              </el-tooltip>
            </el-upload>
            <el-image
              style="
                width: 100px;
                display: inline-block;
                vertical-align: middle;
                height: 36px;
              "
              :src="addForm.img"
              :preview-src-list="[addForm.img]"
              alt=""
            >
              <i class="el-icon-picture-outline"></i>
            </el-image>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleCancel">取 消</el-button>
          <el-button type="primary" @click="handleCommit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<style scoped lang="scss">
.topSearchFive {
  width: 98%;
  margin: 1%;
  background-color: #fff;
  min-height: 75px;
  display: flex;
  justify-content: space-between;

  .topSearchFiveLeft {
    width: 1250px;
    display: flex;
    margin-top: 10px;
    margin-left: 10px;
    > div {
      width: 360px;
      display: flex;
      align-items: center;
      margin-left: 10px;
      p {
        font-size: 14px;
      }
    }
  }

  .topSearchFiveRight {
    width: 200px;
    display: flex;
    align-items: center;
  }
}

.advert {
  width: 98%;
  min-height: 750px;
  margin-top: 1%;
  margin-bottom: 1%;
  margin-left: 1%;
  background-color: #fff;
  overflow: hidden;
  padding: 0 20px;

  .SecondRow {
    width: 100%;
    min-height: 35px;
    margin-top: 20px;
    display: flex;
    justify-content: space-between;

    .SecondRowLeft {
      margin-top: 10px;
      margin-left: 10px;
    }

    .SecondRowRight {
      margin-top: 10px;
      margin-right: 20px;
    }
  }

  .form {
    margin-top: 20px;
  }

  .table {
    margin-top: 20px;
  }
}
</style>
